﻿using System;
using System.Collections.Generic;
using Cirrious.MvvmCross.Plugins.Json;
using Newtonsoft.Json;

namespace TeaLightV5.Foundation.MvvmCross.Plugins.Json
{
    public class TeaJsonConverter : IMvxJsonConverter
    {
        private static readonly JsonSerializerSettings Settings;

        static TeaJsonConverter()
        {
            Settings = new JsonSerializerSettings
            {
                Converters = new List<JsonConverter>
                        {
                            new MvxEnumJsonConverter(),
                        },
                DateFormatHandling = DateFormatHandling.IsoDateFormat,
                TypeNameHandling = TypeNameHandling.Auto
            };
        }

        #region Implementation of IMvxJsonConverter

        public T DeserializeObject<T>(string inputText)
        {
            return JsonConvert.DeserializeObject<T>(inputText, Settings);
        }

        public string SerializeObject(object toSerialise)
        {
            return JsonConvert.SerializeObject(toSerialise, Formatting.None, Settings);
        }

        public object DeserializeObject(Type type, string inputText)
        {
            return JsonConvert.DeserializeObject(inputText, type, Settings);
        }

        #endregion
    }
}